package com.zpself.module.算法练习.String;

/**
 * @author By ZengPeng
 * @Description 罗马数字包含以下七种字符： I， V， X， L，C，D 和 M。
 * 字符          数值
 * I             1
 * V             5
 * X             10
 * L             50
 * C             100
 * D             500
 * M             1000
 * 例如， 罗马数字 2 写做 II ，即为两个并列的 1。12 写做 XII ，即为 X + II 。 27 写做  XXVII, 即为 XX + V + II 。

 * @date in  2021/2/20 10:02
 * @Modified By
 */
public class 力扣_12_整数转罗马数字 {

    public static void main(String[] args) {
        System.out.println(intToRoman(10023));
    }

    //题解：通过双数组来找到对应的字符串，循环比较当前值与数值的大小
    public static String intToRoman(int num) {
        int values[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
        String reps[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};

        StringBuilder result = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            while (num>=values[i]){
                num -= values[i];
                result.append(reps[i]);
            }
        }
        return result.toString();
    }
}
